package com.ruoyi.blindBoxManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.domain.Box;
import com.ruoyi.blindBoxManage.mapper.BlindBoxMapper;
import com.ruoyi.blindBoxManage.service.IBoxService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 盲盒箱子配置Controller
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/box/box")
public class BoxController extends BaseController {
    @Autowired
    private IBoxService boxService;
    @Resource
    private BlindBoxMapper blindBoxMapper;

    /**
     * 查询盲盒箱子配置列表
     */
    @PreAuthorize("@ss.hasPermi('box:box:list')")
    @GetMapping("/list")
    public TableDataInfo list(Box box) {
        if (!StrUtil.isEmpty(box.getBlindBoxName())) {
            Long blindBoxId = blindBoxMapper.selectIdByName(box.getBlindBoxName());
            if (blindBoxId != null) {
                box.setBlindBoxId(blindBoxId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<Box> list = boxService.selectBoxList(box);
        if (!CollectionUtils.isEmpty(list)) {
            for (Box box1 : list) {
                String blindBoxName = blindBoxMapper.selectNameById(box1.getBlindBoxId());
                box1.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出盲盒箱子配置列表
     */
    @PreAuthorize("@ss.hasPermi('box:box:export')")
    @Log(title = "盲盒箱子配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Box box) {
        List<Box> list = boxService.selectBoxList(box);
        if (!CollectionUtils.isEmpty(list)) {
            for (Box box1 : list) {
                String blindBoxName = blindBoxMapper.selectNameById(box1.getBlindBoxId());
                box1.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
            }
        }
        ExcelUtil<Box> util = new ExcelUtil<Box>(Box.class);
        util.exportExcel(response, list, "盲盒箱子配置数据");
    }

    /**
     * 获取盲盒箱子配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('box:box:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Box box = boxService.selectBoxById(id);

        String blindBoxName = blindBoxMapper.selectNameById(box.getBlindBoxId());
        box.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");

        return success(box);
    }

    /**
     * 新增盲盒箱子配置
     */
    @PreAuthorize("@ss.hasPermi('box:box:add')")
    @Log(title = "盲盒箱子配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Box box) {
        return toAjax(boxService.insertBox(box));
    }

    /**
     * 修改盲盒箱子配置
     */
    @PreAuthorize("@ss.hasPermi('box:box:edit')")
    @Log(title = "盲盒箱子配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Box box) {
        return toAjax(boxService.updateBox(box));
    }

    /**
     * 删除盲盒箱子配置
     */
    @PreAuthorize("@ss.hasPermi('box:box:remove')")
    @Log(title = "盲盒箱子配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(boxService.deleteBoxByIds(ids));
    }
}
